<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Metrics\Examples;

use App\Models\User;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class NewUsers extends Line
{
    public $userModel;

    public function __construct()
    {
        parent::__construct();
        $this->userModel = User::query()->get();
    }

    /**
     * 处理请求
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '0':
                $res = $this->calculation(now()->startOfDay(), now()->endOfDay());

                break;
            case '365':
                $res = $this->calculation(now()->startOfYear(), now()->endOfDay());

                break;
            case '30':
                $res = $this->calculation(now()->startOfMonth(), now()->endOfDay());

                break;
            case '90':
                $res = $this->calculation(now()->startOfQuarter(), now()->endOfDay());

                break;
            case '7':
                $res = $this->calculation(now()->startOfWeek(), now()->endOfDay());

                break;
            case '999':
                $res = $this->calculation(now()->startOfDecade(), now()->endOfDay());
                break;
            default:
                $res = $this->calculation(now()->startOfDay(), now()->endOfDay());
                break;
        }
        // 卡片内容
        $this->withContent($res['content']);
        // 图表数据
        $this->withChart($res['chart']);
    }

    /**
     * 设置图表数据.
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }

    /**
     * 初始化卡片内容.
     */
    protected function init()
    {
        parent::init();

        $this->title('新增会员');
        $this->dropdown([
            '0' => '今日',
            '7' => '周',
            '30' => '月',
            '90' => '季度',
            '365' => '年',
            '999' => '全部',
        ]);
    }

    private function calculation(Carbon $start, Carbon $end)
    {
        $total = $this->userModel->whereBetween('created_at', [$start, $end]);
        $info = $total->map(function ($item) {
            $item['date'] = now()->setDateFrom($item->created_at)->format('Y-m-d');
            return $item;
        })->groupBy('date')->map(function ($item) {
            return $item->count();
        });

        return [
            'content' => $total->count(),
            'chart' => $info->values()->all(),
        ];
    }
}
